package com.snail.controller;

import com.snail.model.entity.NoteQueryRequest;
import com.snail.model.entity.NoteSaveRequest;
import com.snail.model.pojo.BizMediaDoc;
import com.snail.model.vo.UserVo;
import com.snail.response.BaseResponse;
import com.snail.response.ResultUtils;
import com.snail.service.BizMediaDocService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/media/doc")
public class BizMediaDocController {
    @Autowired
    private BizMediaDocService bizMediaDocService;


    /**
     * 添加文档
     *
     * @param bizMediaDoc
     */
    @PostMapping("/addDoc")
    public BaseResponse addDoc(@RequestBody BizMediaDoc bizMediaDoc) {
        bizMediaDocService.addDoc(bizMediaDoc);
        return ResultUtils.success();
    }

    /**
     * 根据id获取文档
     *
     * @param id
     * @return
     */
    @GetMapping("/getDocById")
    public BaseResponse getDocById(Integer id) {
        BizMediaDoc bizMediaDoc = bizMediaDocService.getDocById(id);
        return ResultUtils.success(bizMediaDoc);
    }

    /**
     * 根据条件获取文档列表
     *
     * @param bizMediaDoc
     * @return
     */
    @PostMapping("/getDocList")
    public BaseResponse getDocList(BizMediaDoc bizMediaDoc) {
        List<BizMediaDoc> bizMediaDocList = bizMediaDocService.getDocList(bizMediaDoc);
        return ResultUtils.success(bizMediaDocList);
    }

    /**
     * 根据id更新文档
     *
     * @param bizMediaDoc
     * @return
     */
    @PostMapping("/updateDocById")
    public BaseResponse updateDocById(BizMediaDoc bizMediaDoc) {
        bizMediaDocService.updateDocById(bizMediaDoc);
        return ResultUtils.success();
    }

    /**
     * 根据id删除文档
     *
     * @param id
     * @return
     */
    @DeleteMapping("/deleteDocById")
    public BaseResponse deleteDocById(Integer id) {
        if (id == null) {
            return ResultUtils.error("id不能为空");
        } else if (bizMediaDocService.getDocById(id) == null) {
            return ResultUtils.error("文档不存在");
        }
        bizMediaDocService.removeById(id);
        return ResultUtils.success();
    }

    /**
     * 根据用户id获取文档列表
     *
     * @param userVo
     * @return
     */
    @GetMapping("/getDocListByUserId")
    public BaseResponse getDocListByUserId(UserVo userVo) {
        List<BizMediaDoc> bizMediaDocList = bizMediaDocService.getDocListByUserId(userVo);
        return ResultUtils.success(bizMediaDocList);
    }

    /**
     * 保存笔记
     */
    @PostMapping("/save/note")
    public BaseResponse saveNote(@RequestBody NoteSaveRequest noteSaveRequest) {
        bizMediaDocService.saveNote(noteSaveRequest);
        return ResultUtils.success();
    }

    @PostMapping("/get/note")
    public BaseResponse getNote(@RequestBody NoteQueryRequest noteQueryRequest) {
        if (noteQueryRequest == null) {
            return ResultUtils.error("参数不能为空");
        }
        BizMediaDoc bizMediaDoc = bizMediaDocService.getNoteByRequest(noteQueryRequest);
        return ResultUtils.success(bizMediaDoc);
    }

    @PostMapping("/get/notes")
    public BaseResponse getNotesByUser(@RequestBody NoteQueryRequest noteQueryRequest) {
        if (noteQueryRequest == null) {
            return ResultUtils.error("参数不能为空");
        }
        List<BizMediaDoc> bizMediaDocList = bizMediaDocService.getNotesByUser(noteQueryRequest);
        return ResultUtils.success(bizMediaDocList);
    }
}
